﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Ecreate.Web.popedom.userManage
{
    public partial class usertwocrud : System.Web.UI.Page
    {
        Ecreate.Popedom.Service.IUserService userService = new Ecreate.Popedom.Service.SpringImpl.UserServiceImpl();
        Ecreate.Popedom.Service.IGroupService groupService = new Ecreate.Popedom.Service.SpringImpl.GroupServiceImpl();
        Ecreate.Popedom.Service.IUserIdService idService = new Ecreate.Popedom.Service.SpringImpl.UserIdServiceImpl();
        Ecreate.Popedom.Service.IAreaService areaService = new Ecreate.Popedom.Service.SpringImpl.AreaServiceImpl();
        Ecreate.Popedom.Service.SpringImpl.UseStateImpl useState = new Ecreate.Popedom.Service.SpringImpl.UseStateImpl();

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!this.IsPostBack)
            {
                this.txt_id.ReadOnly = true;
                this.ddl_group.DataSource = groupService.getAllTwo();
                this.ddl_group.DataTextField = "Groupname";
                this.ddl_group.DataValueField = "Id";
                this.ddl_group.DataBind();

                this.ddl_province.DataSource = areaService.getProvince();
                this.ddl_province.DataTextField = "AreaName";
                this.ddl_province.DataValueField = "Id";
                this.ddl_province.DataBind();


                string value = this.ddl_province.SelectedValue;
                this.ddl_city.DataSource = areaService.getCity(value);
                this.ddl_city.DataTextField = "AreaName";
                this.ddl_city.DataValueField = "Id";
                this.ddl_city.DataBind();



                this.ddl_usestate.DataSource = useState.getAllState();
                this.ddl_usestate.DataTextField = "Name";
                this.ddl_usestate.DataValueField = "Id";
                this.ddl_usestate.DataBind();



                this.ddl_parentuser.DataSource = userService.getAllOne();
                this.ddl_parentuser.DataTextField = "Username";
                this.ddl_parentuser.DataValueField = "Id";
                this.ddl_parentuser.DataBind();


                this.hid_sendurl.Value = this.Request.UrlReferrer.AbsoluteUri;
                string action = this.Request.QueryString["action"];
                string id = this.Request.QueryString["id"];
                this.hid_action.Value = action;
                this.hid_id.Value = id;
                if (action == ActionType.delete.ToString())
                {
                    Ecreate.Popedom.tSysuser user = userService.getById(id);
                    if (user.Group.Adminflag == 1)
                    {
                        this.Response.Redirect(this.hid_sendurl.Value);
                        return;
                    }
                    userService.DeleteUser(user);
                    this.Response.Redirect(this.hid_sendurl.Value);
                }
                if (action == ActionType.edit.ToString())
                {
                    Ecreate.Popedom.tSysuser user = userService.getById(id);
                    if (user.Group.Adminflag == 1)
                    {
                        Response.Write("<script type=\"text/javascript\" language=\"javascript\">     alert('管理员组不允许修改!')　　; </script>");
                        this.Response.Redirect(this.hid_sendurl.Value);
                        return;
                    }
                    this.ddl_group.SelectedValue = user.Group.Id.ToString();
                    this.ddl_province.SelectedValue = user.Province.Id.ToString();
                    this.ddl_usestate.SelectedValue = user.UseFlag.ToString();

                    this.ddl_city.DataSource = areaService.getCity(this.ddl_province.SelectedValue);
                    this.ddl_province.DataTextField = "AreaName";
                    this.ddl_province.DataValueField = "Id";
                    this.ddl_city.DataBind();

                    if (user.ParentUser != null)
                    {
                        this.ddl_parentuser.SelectedValue = user.ParentUser.Id.ToString();
                    }

                    this.txt_name.Text = user.Username;
                    this.txt_id.Text = user.Id.ToString();
                    this.txt_id.ReadOnly = true;
                    this.txt_password.Text = user.Password;

                    this.txt_address.Text = user.Address;
                    this.txt_tele.Text = user.Tele;
                    this.txt_account.Text = user.Account;
                    this.txt_qq.Text = user.Qq;
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string Groupid = this.ddl_group.SelectedValue;
            Ecreate.Popedom.tGroup group = groupService.getById(long.Parse(Groupid));
            Ecreate.Popedom.tArea area = areaService.getById(ddl_province.SelectedValue);
            Ecreate.Popedom.tArea city = areaService.getById(ddl_city.SelectedValue);
            Ecreate.Popedom.tSysuser parentuser = userService.getById(ddl_parentuser.SelectedValue);

            string useflag = this.ddl_usestate.SelectedValue.ToString();

            if (this.hid_action.Value == ActionType.add.ToString())
            {
                Ecreate.Popedom.tSysuser user = new Ecreate.Popedom.tSysuser();
                user.Province = area;
                user.City = city;
                user.Id = userService.getByTwoId(user);
                user.Username = this.txt_name.Text;
                user.Group = group;
                user.Password = this.txt_password.Text;
                user.FLevel = 2;
                user.UseFlag = int.Parse(useflag);

                user.Address = this.txt_address.Text;
                user.Account = this.txt_account.Text;
                user.Tele = this.txt_tele.Text;
                user.Qq = this.txt_qq.Text;
                user.ParentUser = parentuser;
                //useflagmanage.setCancelDate(user); 
                try
                {
                    userService.InsertUser(user);
                    HtmlHelper.ShowAlertMessage(this, "添加成功!登陆Id为" + user.Id.ToString());
                }
                catch (Exception ex)
                {
                    HtmlHelper.ShowAlertMessage(this, ex.ToString());
                    return;
                }
            }
            if (this.hid_action.Value == ActionType.edit.ToString())
            {
                Ecreate.Popedom.tSysuser user = new Ecreate.Popedom.tSysuser();
                user.Id = this.hid_id.Value;
                user.Username = this.txt_name.Text;
                user.Group = group;
                user.Password = this.txt_password.Text;
                user.Province = area;
                user.City = city;
                user.FLevel = 2;
                user.UseFlag = int.Parse(useflag);
                user.Address = this.txt_address.Text;
                user.Account = this.txt_account.Text;
                user.Tele = this.txt_tele.Text;
                user.Qq = this.txt_qq.Text;
                user.ParentUser = parentuser;
                useflagmanage.setCancelDate(user); 
                userService.UpdateUser(user);
            }
            this.Response.Redirect(this.hid_sendurl.Value);
        }

        protected void ddl_province_SelectedIndexChanged(object sender, EventArgs e)
        {

            string value = this.ddl_province.SelectedValue;
            IList<Ecreate.Popedom.tArea> areas=this.areaService.getCity(value);
            this.ddl_city.DataSource = areas;
            this.ddl_city.DataBind();

        }
    }
}
